(function(window) {
    var s = {}
        // 验证手机号
    s.phone = function(phone) {
            if (!phone) {
                api.toast({
                    msg: '手机号不能为空',
                    duration: 2000,
                    location: 'middle'
                });
                return false
            }
            // if ((phone.length < 11)||(phone.length > 14)) {
            //     api.toast({
            //         msg: '请输入正确格式的手机号',
            //         duration: 2000,
            //         location: 'middle'
            //     });
            //     return false
            // }
            // var phoneReg = /^\d{0,4}?(1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
            // var mobilePatternTW = /^(\+00886\s)?[0]{1}[9]{1}\d{8}$/;
            //
            // var flag1 = phoneReg.test(phone);
            // var flag2 = mobilePatternTW.test(phone);
            // if (!flag1 && !flag2) {
            //     api.toast({
            //         msg: '请输入正确格式的手机号',
            //         duration: 2000,
            //         location: 'middle'
            //     });
            //     return false
            // }
            return true
        }
        // 验证密码
    s.password = function(password) {
        if (!password) {
            api.toast({
                msg: '密码不能为空',
                duration: 2000,
                location: 'middle'
            });
            return false
        }
        if (password.length > 16) {
            api.toast({
                msg: '密码不能超过16位！',
                duration: 2000,
                location: 'middle'
            });
            return false
        }
        if (password.length < 6 || password.length > 16) {
            api.toast({
                msg: '密码格式不正确！',
                duration: 2000,
                location: 'middle'
            });
            return false
        }
        return true
    }

    s.yanzhengma = function(msg) {
            if (!msg) {
                api.toast({
                    msg: '请输入验证码',
                    duration: 2000,
                    location: 'middle'
                });
                return false
            }
            return true

        }
        // 倒计时
    s.daojishi = function(msgflag, name) {
        var settime = 59;
        var timeid = null;
        var connectionType = api.connectionType;
        if (connectionType == "none") {
            api.toast({
                msg: '请检查网络设置',
                duration: 2000,
                location: 'middle'
            });
            return;
        }
        if (!msgflag) {
            $('.' + name).val('获取验证码')
        } else {
            var button = $('.codeactive');
            // var button = $('.' + name);
            button.val(settime + 's');
            button.attr("disabled", true);
            timeid = setInterval(function() {
                settime--;
                button.val( settime + 's');
                if (settime <= 0) {
                    button.val('获取验证码')
                    button[0].removeAttribute('disabled');
                    clearInterval(timeid);

                    settime = 59;
                }
            }, 1000)
        }
    }
    s.getlogin = function() {
        var session = $api.getStorage('session');
        var uid = $api.getStorage('uid');
        // var mobile = $api.getStorage('mobile');
        // var sessiontime = $api.getStorage('sessiontime');
        if (session && uid) {
            return true
        } else {
            return false
        }
    }
    s.getUserInfo = function(goback) {
        if (s.getlogin()) {
            new rest().get('getUserInfo').datae({
                'uid': uid,
            }).success(function(res) {
                $api.setStorage('mobile', res.msg[0].mobile);
                $api.setStorage('portrait', res.msg[0].http);
                $api.setStorage('code', res.msg[0].code);
                $api.setStorage('nickname', res.msg[0].nickname);
                if (typeof goback == 'function') {
                    goback(res)
                }
            }).error(function(err) {
                console.log(JSON.stringify(err) + 'login')
            }).run()
        } else {
            console.log("用户未登录");
        }
    }

    window.M = s;
})(window)
